﻿using System.Windows;
using System.Windows.Controls;
using compshop.Modules.Categories.Presenter;
using compshop.Infrastructure.CompShopDataService;

namespace compshop.Modules.Categories.Views
{
    /// <summary>
    /// Interaction logic for Categories.xaml
    /// </summary>
    public partial class CategoriesView : UserControl, IViewCategory
    {
        public CategoriesView()
        {
            InitializeComponent();

            Button btnAddCategory = new Button();
            btnAddCategory.Content = Resource.NewCategory;
            btnAddCategory.HorizontalAlignment = HorizontalAlignment.Right;
            btnAddCategory.VerticalAlignment = VerticalAlignment.Bottom;
            btnAddCategory.Margin = new Thickness(2, 3, 236, 3);
            btnAddCategory.Width = 120;
            btnAddCategory.Click += btnAddCategory_Click;
            gridMain.Children.Add(btnAddCategory);
            Grid.SetRow(btnAddCategory, 1);

            Button btnAddSubCategory = new Button();
            btnAddSubCategory.Content = Resource.NewSubCategory;
            btnAddSubCategory.HorizontalAlignment = HorizontalAlignment.Right;
            btnAddSubCategory.VerticalAlignment = VerticalAlignment.Bottom;
            btnAddSubCategory.Margin = new Thickness(2, 3, 104, 3);
            btnAddSubCategory.Width = 130;
            btnAddSubCategory.Click += btnAddSubCategory_Click;
            gridMain.Children.Add(btnAddSubCategory);
            Grid.SetRow(btnAddSubCategory, 1);

            Button btnEdit = new Button();
            btnEdit.Content = Resource.Edit;
            btnEdit.HorizontalAlignment = HorizontalAlignment.Right;
            btnEdit.VerticalAlignment = VerticalAlignment.Bottom;
            btnEdit.Margin = new Thickness(2, 3, 2, 3);
            btnEdit.Width = 100;
            btnEdit.Click+=new RoutedEventHandler(btnEdit_Click);
            gridMain.Children.Add(btnEdit);
            Grid.SetRow(btnEdit, 1);
        }


        public CategoriesView(CategoriesPresenter presenter):this()
        {
            Presenter = presenter;
            Presenter.View = this;
        }

        public CategoriesPresenter Presenter
        {
            get { return DataContext as CategoriesPresenter; }
            set 
            { 
                DataContext = value;
                treeviewCategories.ItemsSource = Presenter.CategoryList;
            }
        }


        private void treeviewCategories_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is SubCategory)
            {
                SubCategory subCat = (SubCategory)e.NewValue;
                Presenter.SubCategorySelectedPublisher(subCat.SubCategoryID);
            }
            else
                if (e.NewValue is Category)
                {
                    Category cat = (Category)e.NewValue;
                    Presenter.CategorySelectedPublisher(cat.CategoryID);
                }
        }

        private void btnAddCategory_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ShowAddCategoryDialog();
        }

        private void btnAddSubCategory_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ShowAddSubCategoryDialog();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {     
            if (treeviewCategories.SelectedItem == null)
            {
                MessageBox.Show(Resource.NoChange);
                return;
            }
            if (treeviewCategories.SelectedItem is TreeViewItem)
            {
                TreeViewItem item = treeviewCategories.SelectedItem as TreeViewItem;
                Presenter.ShowEditDialog(item.DataContext);
            }
            else
                Presenter.ShowEditDialog(treeviewCategories.SelectedItem);           
        }

    }
}






//HierarchicalDataTemplate template = new HierarchicalDataTemplate(typeof(Category));
//template.ItemsSource = new Binding("SubCategories");

//FrameworkElementFactory factoryTextBlock = new FrameworkElementFactory(typeof(TextBlock));
//factoryTextBlock.SetBinding(TextBlock.TextProperty, new Binding("Name"));
//template.VisualTree = factoryTextBlock;

//foreach (Category cat in Presenter.CategoryList)
//{
//    TreeViewItem item = new TreeViewItem();
//    item.DataContext = cat;
//    item.Header = cat.Name; ;
//    //item.DisplayMemberPath = "Name";
//    item.ItemsSource = cat.SubCategories;
//    item.ItemTemplate = template;
//    treeviewCategories.Items.Add(item);
//    //item.IsExpanded = true;
//}